package net.huashitong.ssydt.provider.common.service;


import net.huashitong.ssydt.base.dao.LongMapper;
import net.huashitong.ssydt.base.service.AbstractLongService;
import net.huashitong.ssydt.common.entity.SsCommonNotice;
import net.huashitong.ssydt.common.entity.vo.SsCommonNoticeQueryVO;
import net.huashitong.ssydt.exam.entity.SsExamQuestion;
import net.huashitong.ssydt.msg.constant.MessageConsts;
import net.huashitong.ssydt.provider.common.mapper.SsCommonNoticeMapper;
import net.huashitong.ssydt.provider.msg.dto.SsApplicationMessage;
import net.huashitong.ssydt.provider.msg.service.MsgPushService;
import net.huashitong.user.results.PaginationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.11.22
 */
@Service
@Transactional
public class SsCommonNoticeServiceImpl extends AbstractLongService<SsCommonNotice> implements SsCommonNoticeService {

    private SsCommonNoticeMapper mapper;

    private MsgPushService msgPushService;

    @Autowired
    public SsCommonNoticeServiceImpl(SsCommonNoticeMapper mapper,
                                     MsgPushService msgPushService) {
        this.mapper = mapper;
        this.msgPushService = msgPushService;
    }

    @Override
    protected LongMapper<SsCommonNotice> getLongMapper() {
        return this.mapper;
    }


    @Override
    public SsCommonNotice getFirst() {
        return this.mapper.getFirst();
    }

    @Override
    public PaginationResult<SsCommonNotice> paginationQuery(SsCommonNoticeQueryVO queryVO) {
        PaginationResult result = new PaginationResult();
        result.setPageNumber(queryVO.getPageNumber());
        result.setPageSize(queryVO.getPageSize());

        // 查询记录总数
        int total = mapper.paginationCount(queryVO);
        result.setPageRecords(total);

        if (total == 0) {
            result.setList(new ArrayList());
        } else {
            // 查询当前页记录
            List<SsExamQuestion> list = mapper.paginationQuery(queryVO);
            result.setList(list);
        }
        return result;
    }

    @Override
    public void postPushMessage(Long id) {
        SsCommonNotice ssCommonNotice = selectByPrimaryKey(id);
        if (ssCommonNotice != null) {
            SsApplicationMessage message = new SsApplicationMessage();
            message.setMsgType(MessageConsts.MsgType.SYSTEM);
            message.setTargetType(MessageConsts.TargetType.WEB_PAGE);
            message.setTarget(ssCommonNotice.getLinkUrl());
            message.setTitle("公告消息");
            message.setText(ssCommonNotice.getTitle());
            message.setSender("admin");
            msgPushService.pushMessageToApplication(message);
        }
    }

    @Override
    public void logicalDelete(Long id) {
        SsCommonNotice ssCommonNotice = selectByPrimaryKey(id);
        ssCommonNotice.setIsDelete((byte) 1);
        updateByPrimaryKey(ssCommonNotice);
    }
}
